package manage;

import logIn.MysqlConnectionTool;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 查询全部员工信息显示面板
 * @Author Jian Chen long
 * @Date 2021/6/17 21:21
 */
public class allWorker extends JFrame {
    // 显示面板
    private JTextArea select = new JTextArea();
    /**
     * 当信息过多时,增加滚动条
     */
    private JScrollPane pane = new JScrollPane();

    public allWorker(){
        pane.setViewportView(select);
        select.setEditable(false);
        this.add(pane);

        this.setTitle("员工信息");
        select.setText("编号\t" + "姓名\t" + "联系方式\n");
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.setSize(300,280);
        this.setLocationRelativeTo(null);
        this.setResizable(false);

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            conn = MysqlConnectionTool.getConnection();
            // 获取数据库操作对象
            String sql = "select * from userview";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                select.append(rs.getString("编号") + "\t"
                        + rs.getString("姓名") + "\t"
                        + rs.getString("联系方式") + "\n");
            }
        }catch (Exception E){
            JOptionPane.showMessageDialog(null,"查询失败");
            return;
        }finally {
            MysqlConnectionTool.close(conn,ps,rs);
        }
    }
}
